package org.example.infrastructure.persistent.repository;

import com.alibaba.fastjson.JSON;
import org.example.domain.activity_manage.model.entity.ActivityEntity;
import org.example.domain.activity_manage.repository.IActivityRepo;
import org.example.infrastructure.persistent.dao.IActivityDao;
import org.example.infrastructure.persistent.po.ActivityPO;
import org.example.types.common.RedisKeysPrefix;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lxc18
 * @date 2025/3/17 19:59
 * @description ActivityRepo
 */
@Repository
public class ActivityRepo implements IActivityRepo {
    @Resource
    private RedissonClient redissonClient;

    @Resource
    private IActivityDao activityDao;
    @Override
    public boolean createActivity(ActivityEntity activityEntity) {
        ActivityPO po = ActivityPO.builder()
                .activityId(activityEntity.getActivityId())
                .activityName(activityEntity.getActivityName())
                .startTime(activityEntity.getStartTime())
                .endTime(activityEntity.getEndTime())
                .build();
        if(activityDao.createActivity(po)){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateActivity(ActivityEntity activityEntity) {
        ActivityPO po = ActivityPO.builder()
                .activityId(activityEntity.getActivityId())
                .activityName(activityEntity.getActivityName())
                .startTime(activityEntity.getStartTime())
                .endTime(activityEntity.getEndTime())
                .createTime(activityEntity.getCreateTime())
                .updateTime(activityEntity.getUpdateTime())
                .build();
        boolean updated = activityDao.updateActivity(po);
        if(updated){
            //删除缓存中活动信息
            String cacheKey = RedisKeysPrefix.ACTIVITY_INFO + activityEntity.getActivityId();
            try {
                redissonClient.getBucket(cacheKey).delete();
            } catch (Exception e) {
                redissonClient.getBucket(cacheKey).expire(1, TimeUnit.SECONDS);
            }
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean deleteActivity(Long activityId) {
        boolean deleted = activityDao.deleteActivity(activityId);
        if(deleted){
            //删除缓存中活动信息
            String cacheKey = RedisKeysPrefix.ACTIVITY_INFO + activityId;
            try {
                redissonClient.getBucket(cacheKey).delete();
            } catch (Exception e) {
                redissonClient.getBucket(cacheKey).expire(1, TimeUnit.SECONDS);
            }
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public List<ActivityEntity> queryActivityByName(String activityName) {
        //不放到数据库中，因为数据量不大，查询频率也不高
        List<ActivityPO> activityPOS = activityDao.queryActivityByName(activityName);
        ArrayList<ActivityEntity> res = new ArrayList<>();
        for (ActivityPO activityPO : activityPOS) {
            ActivityEntity activityEntity = ActivityEntity.builder()
                        .activityId(activityPO.getActivityId())
                        .activityName(activityPO.getActivityName())
                        .startTime(activityPO.getStartTime())
                        .endTime(activityPO.getEndTime())
                        .createTime(activityPO.getCreateTime())
                        .updateTime(activityPO.getUpdateTime())
                        .build();
            res.add(activityEntity);
        }
        return res;
    }

    @Override
    public ActivityEntity queryActivityById(Long activityId) {
        //先去缓存里面查找，如果缓存里面没有，再去数据库里面查找
        String cacheKey = RedisKeysPrefix.ACTIVITY_INFO + activityId;
        String jsonStr = (String) redissonClient.getBucket(cacheKey).get();
        ActivityEntity activityEntity = JSON.parseObject(jsonStr, ActivityEntity.class);
        if(activityEntity == null){
            ActivityPO po = activityDao.queryActivityById(activityId);
            if(po == null){
                return null;
            }
            activityEntity = ActivityEntity.builder()
                        .activityId(po.getActivityId())
                        .activityName(po.getActivityName())
                        .startTime(po.getStartTime())
                        .endTime(po.getEndTime())
                        .createTime(po.getCreateTime())
                        .updateTime(po.getUpdateTime())
                        .build();
            //将活动信息放入缓存
            redissonClient.getBucket(cacheKey).set(JSON.toJSONString(activityEntity));
        }
        return activityEntity;
    }

    @Override
    public List<ActivityEntity> queryActivityByPage(int pageNum, int pageSize) {
        List<ActivityPO> activityPOS = activityDao.queryActivityByPage(pageNum, pageSize);
        List<ActivityEntity> res = new ArrayList<>();
        for (ActivityPO activityPO : activityPOS) {
            ActivityEntity activityEntity = ActivityEntity.builder()
                        .activityId(activityPO.getActivityId())
                        .activityName(activityPO.getActivityName())
                        .startTime(activityPO.getStartTime())
                        .endTime(activityPO.getEndTime())
                        .createTime(activityPO.getCreateTime())
                        .updateTime(activityPO.getUpdateTime())
                        .build();
            res.add(activityEntity);
        }
        return res;
    }
}
